'use strict';

const Controller = require('egg').Controller;

class GiftController extends Controller {
  async list() {
    let page = this.ctx.request.body.page ? parseInt(this.ctx.request.body.page) : 1
    let limit = parseInt(this.ctx.request.body.limit);
    console.log(page, limit)
    let offset = (page - 1) * limit;
    const res = await this.app.model.Gift.findAll({
      offset,
      limit
    })
    let count = await this.app.model.Gift.count();
    console.log(count, "count")
    let obj = {
      data: res,
      count: count
    }
    this.ctx.apiSuccess(
      obj, "获取成功", 200
    )
  }
  async add() {

    this.ctx.validate({
      name: {
        type: "string",
        required: true,
        defValue: "",
        desc: "礼物名称",
      },
      image: { type: "string", required: true, desc: "礼物图标" },
      coin: {
        type: "int",
        required: true,
        defValue: "",
        desc: "金币",

      },
    })
    if (await this.app.model.Gift.findOne({
      where: {
        name: this.ctx.request.body.name
      }
    })) {
      this.ctx.throw("该礼物已存在", "该礼物已存在", 400)
    }
    let res = await this.app.model.Gift.create({
      name: this.ctx.request.body.name,
      image: this.ctx.request.body.image,
      coin: this.ctx.request.body.coin
    })
    if (!res) {
      this.ctx.throw(res, "创建失败", 500)
    }
    this.ctx.apiSuccess(res, "创建成功", 200)
  }
  async edit() {

    this.ctx.validate({
      id: {
        type: "int",
        required: true,
        defValue: "",
        desc: "礼物id",
      },
      name: {
        type: "string",
        required: true,
        defValue: "",
        desc: "礼物名称",
      },
      image: { type: "string", required: true, desc: "礼物图标" },
      coin: {
        type: "int",
        required: true,
        defValue: "",
        desc: "金币",

      },
    })
    let res = await this.app.model.Gift.findOne({
      where: {
        id: this.ctx.request.body.id
      }
    })
    if (!res) {
      this.ctx.throw(res, "该礼物不存在", 500)
    }
    res.name = this.ctx.request.body.name;
    res.image = this.ctx.request.body.image;
    res.coin = this.ctx.request.body.coin;

    res.save()
    this.ctx.apiSuccess(res, "修改成功", 200)
  }
  async deleteGift() {
    this.ctx.validate({
      id: {
        type: "int",
        required: false,
        defValue: "",
        desc: "id",

      },
    })
    let res = this.app.model.Gift.destroy({
      where: {
        id: this.ctx.request.body.id
      }
    })
    this.ctx.apiSuccess(res, "删除成功", 200)
  }
  //微信支付
  async wxpay() {
    let user_id = this.ctx.authUser.id;
    this.ctx.validate({
      price: {
        type: "int",
        required: true,
        desc: "充值费用"
      },
      type: {
        type: "string", required: true, desc: "类型"
      },
    })
    const price = this.ctx.request.body.price;
    if (price < 1) {
      return this.ctx.apiFail("至少充值1元")
    }
    let no = this.ctx.randomString(20);
    console.log(no, "no")
    let res = await this.app.model.Order.create({
      no,
      user_id,
      price
    })
    console.log(res, "res")
    if (!res) {
      return this.ctx.apiFail("创建订单失败")
    }
    console.log(price, "pre")
    const result = await this.app.tenpay.getAppParams({
      out_trade_no: no,
      body: "支付测试",
      total_fee: price * 100,
      trade_type: "APP"
    })
    console.log(result, "result")
    let user = await this.app.model.User.findOne({
      where: {
        id: user_id
      }
    });
    if (user) {
      user.coin += parseInt(price);
      user.save()
    }
    this.ctx.apiSuccess(result, "创建订单成功", 200)
  }
  async notify() {
    let info = this.ctx.request.weixin;
    if (!info || info.result_code !== 'SUCCESS') {
      return this.ctx.apiFail("支付失败")
    }
    let order = await this.app.model.Order.findOne({
      where: {
        no: info.out_trade_no
      }
    })
    if (!order) {
      return this.ctx.apiFail("订单不存在")
    }
    order.status = "success"
    order.save();
    let user = await this.service.user.exist(order.user_id);
    if (user) {
      user.coin += parseInt(info.total_fee) / 100;
      user.save()
    }
    this.ctx.reply();
  }
}

module.exports = GiftController;
